<template>
	<view>
		<view class="cart-container" v-if="cart.length !== 0">
			<!-- 选择地址组件 -->
			<my-address></my-address>
			<!-- 标题 -->
			<view class="cart-title">
				<uni-icons type='shop-filled' size="18"></uni-icons>
				<text class="title-text">购物车</text>
			</view>
			<!-- 商品列表区域 -->
			<!-- uni-swipe-action 是最外层包裹性质的容器 -->
			<uni-swipe-action>
				<block v-for="(goods,i) in cart" :key="i">
					<!-- uni-swipe-action-item 可以为其子节点提供滑动操作的效果。需要通过 options 属性来指定操作按钮的配置信息 -->
					<uni-swipe-action-item :right-options="options" :auto-close="false"
						@click="swipeActionClickHandler(goods)">
						<my-goods :goods="goods" :showRadio="true" :showNum="true" @radio-change='radioChangeHandler'
							@num-change="numChangeHandler"></my-goods>
					</uni-swipe-action-item>
				</block>
			</uni-swipe-action>
			<!-- 使用自定义的结算组件 -->
			<my-settle></my-settle>
		</view>
		<view class="cart-bg-box" v-else>
			<image src="/static/cartNull.png" class="empty-img"></image>
			<text class="tip-text">空空如也~</text>
		</view>
	</view>

</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		computed: {
			// 将m_cart模块里的total引入到当前页面的计算属性
			...mapState('m_cart', ['cart'])
		},
		mixins: [badgeMix],
		data() {
			return {
				list: [],
				show: false,
				isOpened: 'none',
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#f51909'
					}
				}],
			};
		},
		onLoad() {
			console.log('购物车商品列表数据', this.cart);
		},
		methods: {
			...mapMutations('m_cart', ['updataGoodsState', 'updataGoodsCount', 'removeGoodsById']),
			// 商品的选中状态发生了变化
			radioChangeHandler(e) {
				this.updataGoodsState(e)
			},
			// 商品的数量发生了变化
			numChangeHandler(e) {
				// console.log(e);
				this.updataGoodsCount(e)
			},
			// 删除商品
			// 点击了滑动操作按钮
			swipeActionClickHandler(goods) {
				this.removeGoodsById(goods.goods_id)
			}
		}
	}
</script>

<style lang="scss">
	.cart-container {
		padding-bottom: 3rem;

		.cart-title {
			height: 2.5rem;
			display: flex;
			align-items: center;
			padding-left: 0.3125rem;

			.title-text {
				margin-left: 0.625rem;
				font-size: 14px;
			}
		}
	}

	.cart-bg-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 50px;

		.empty-img {
			width: 290px;
			height: 390px;
		}

		.tip-text {
			font-size: 16px;
			color: gray;
		}
	}
</style>
